package com.gz.mylife.app.llopendoor;

import java.lang.ref.WeakReference;


import org.json.JSONObject;
import org.xutils.x;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gz.mylife.App;
import com.gz.mylife.R;
import com.gz.mylife.app.BaseActivity;
import com.gz.mylife.util.PhoneSN;
import com.gz.mylife.util.PrefUtils;
import com.zl.smartmall.library.util.Constants;
public class OpenActivationActivity extends BaseActivity implements OnClickListener{
	private EditText et_acitvenum;//激活码
	private EditText et_phone;//用户手机号
	private Button btn_login;
	private TextView tv_resend;//获取验证码的按钮文字
	private MyHandler mHandler;
	private EditText ex_aldphone;//重新验证的手机号
	private int time = 59;
	private String phone;
	private EditText et_sms;//验证码
	private Button btn_verification;//重新验证按钮
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_doorlogin);
		setActionbarTitle("门禁激活", false);
		initview();
	}
	//初始化控件
	private void initview(){
		mHandler = new MyHandler(this);
		et_acitvenum = (EditText) findViewById(R.id.et_acitvenum);
		et_phone = (EditText) findViewById(R.id.et_phone);
		btn_login = (Button) findViewById(R.id.btn_login);
		btn_login.setOnClickListener(this);
		tv_resend = (TextView) findViewById(R.id.tv_resend);
		tv_resend.setOnClickListener(this);
		ex_aldphone = (EditText) findViewById(R.id.ex_aldphone);
		et_sms = (EditText) findViewById(R.id.et_sms);
		btn_verification = (Button) findViewById(R.id.btn_verification);
		btn_verification.setOnClickListener(this);
	}
	/**
	 * 激活
	 */
	private void active() {
		String url = Constants.ACTIVITY;
		RequestParams params = new RequestParams(url);
		params.addParameter("active_sn", et_acitvenum.getText().toString().trim());//激活码
		params.addParameter("phone", et_phone.getText().toString().trim());//手机号
		Log.i("duzhijuan", params + "");
		x.http().get(params, new Callback.CommonCallback<String>() {

			@Override
			public void onCancelled(CancelledException arg0) {
				// TODO Auto-generated method stub
				Log.e("cancelled", arg0.toString());
			}

			@Override
			public void onError(Throwable arg0, boolean arg1) {
				// TODO Auto-generated method stub
				Log.e("onError", arg0.toString());
			}

			@Override
			public void onFinished() {
				// TODO Auto-generated method stub
				Log.e("onFinished","onFinished");
			}

			@Override
			public void onSuccess(String result) {
				// TODO Auto-generated method stub
				try {
					JSONObject jsonObject = new JSONObject(result);
					if ("0".equals(jsonObject.getString("ref"))) {
						Toast.makeText(OpenActivationActivity.this, "激活成功！", Toast.LENGTH_LONG).show();
						PrefUtils.setStrPrefData(OpenActivationActivity.this, "user_phone", et_phone.getText().toString().trim());
						login(1);
					}else{
						Toast.makeText(OpenActivationActivity.this, jsonObject.getString("msg"), Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
				
			}



		});
	}
	
	/**
	 * 登录
	 */
	private void login(final int type) {
		
		String url = Constants.OPENLOGIN;
		RequestParams params = new RequestParams(url);
		if (type == 1) {//等于一说明是激活成功后直接调用登录，相反则说明是重新验证的手机号
			params.addParameter("phone", et_phone.getText().toString().trim());//手机号
			params.addParameter("code", "");//验证码
		}else{
			params.addParameter("phone", ex_aldphone.getText().toString().trim());//手机号
			params.addParameter("code", et_sms.getText().toString().trim());//验证码
		}
		params.addParameter("phone_sn", PhoneSN.getPhoneSN(OpenActivationActivity.this));//手机唯一标识
		Log.i("duzhijuan", params + "");
		x.http().get(params, new Callback.CommonCallback<String>() {

			@Override
			public void onCancelled(CancelledException arg0) {
				// TODO Auto-generated method stub
				Log.e("cancelled", arg0.toString());
			}

			@Override
			public void onError(Throwable arg0, boolean arg1) {
				// TODO Auto-generated method stub
				Log.e("onError", arg0.toString());
			}

			@Override
			public void onFinished() {
				// TODO Auto-generated method stub
				Log.e("onFinished","onFinished");
			}

			@Override
			public void onSuccess(String result) {
				// TODO Auto-generated method stub
				try {
					JSONObject jsonObject = new JSONObject(result);
					if ("0".equals(jsonObject.getString("ref"))) {
						PrefUtils.setStrPrefData(OpenActivationActivity.this, "linglingID", jsonObject.getString("result"));
						if (type == 1) {
							PrefUtils.setStrPrefData(OpenActivationActivity.this, "user_phone", et_phone.getText().toString().trim());
						}else{
							PrefUtils.setStrPrefData(OpenActivationActivity.this, "user_phone", ex_aldphone.getText().toString().trim());
						}
						
						finish();
						 //发送广播
                        Intent intent = new Intent("updateUI");
                        sendBroadcast(intent);
					}else{
						Toast.makeText(OpenActivationActivity.this, jsonObject.getString("msg"), Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
				
			
		});
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.btn_login:
			if (et_acitvenum.getText().toString().trim().length()==0) {
				Toast.makeText(OpenActivationActivity.this, "请输入正确的激活码", Toast.LENGTH_LONG).show();
			}else if(et_phone.getText().toString().trim().length()==0){
				Toast.makeText(OpenActivationActivity.this, "请输入正确的手机号", Toast.LENGTH_LONG).show();
			}else{
				active();
			}
			break;
		case R.id.tv_resend:
		phone = ex_aldphone.getText().toString().trim();

		if (0==phone.length()||11!=phone.length()) {
			App.mTimeState = false;
			Toast.makeText(this, "请正确输入手机号！！！", 0).show();
			return;
		}else{
			tv_resend.setText("("+time+")后重新获取");
			mHandler.postDelayed(leftTime, 1000);
		}
		sms();
		break;
		case R.id.btn_verification:
			if (ex_aldphone.getText().toString().trim().length()!=11) {
				Toast.makeText(OpenActivationActivity.this, "请输入正确的手机号", Toast.LENGTH_LONG).show();
			}else if(et_sms.getText().toString().trim().length()==0){
				Toast.makeText(OpenActivationActivity.this, "请输入正确的验证码", Toast.LENGTH_LONG).show();
			}else{
				login(2);
			}
			break;
		default:
			break;
		}
	}
	private Runnable leftTime = new Runnable() {

		@Override
		public void run() {
			if(time>0){
				tv_resend.setEnabled(false);
				tv_resend.setTextColor(getResources().getColor(R.color.divid_item_gray));
				tv_resend.setText("("+time+")后重新获取");
				mHandler.postDelayed(leftTime, 1000);
				time--;
			}else{
				time = 59;
				tv_resend.setEnabled(true);
				tv_resend.setTextColor(getResources().getColor(R.color.RGB_ff8018));
				tv_resend.setText("重新获取");
				mHandler.removeCallbacks(leftTime);
			}

		}
	};
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mHandler.removeCallbacks(leftTime);
		mHandler.release();
	}
	private static class MyHandler extends Handler {
		private OpenActivationActivity mOuter;

		public MyHandler(OpenActivationActivity outer) {
			this.mOuter = outer;
		}

		public void release() {
			new WeakReference<OpenActivationActivity>(mOuter);
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				break;

			default:
				break;
			}
		}
	}
	/**
	 * 请求验证码
	 */
	private void sms() {
		
		String url = Constants.GET_EMS;
		RequestParams params = new RequestParams(url);
		params.addParameter("phone", ex_aldphone.getText().toString().trim());//手机号
		Log.i("duzhijuan", params + "");
		x.http().get(params, new Callback.CommonCallback<String>() {

			@Override
			public void onCancelled(CancelledException arg0) {
				// TODO Auto-generated method stub
				Log.e("cancelled", arg0.toString());
			}

			@Override
			public void onError(Throwable arg0, boolean arg1) {
				// TODO Auto-generated method stub
				Log.e("onError", arg0.toString());
			}

			@Override
			public void onFinished() {
				// TODO Auto-generated method stub
				Log.e("onFinished","onFinished");
			}

			@Override
			public void onSuccess(String result) {
				// TODO Auto-generated method stub
				try {
					JSONObject jsonObject = new JSONObject(result);
					if ("1".equals(jsonObject.getString("ref"))) {
						Toast.makeText(OpenActivationActivity.this, jsonObject.getString("msg"), Toast.LENGTH_LONG).show();
					}else{
						
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
				
			
		});
	}
	
}
